import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isFunction from "lodash/isFunction";

var _dec, _dec2, _class;

/**
 * 常用功能 集合
 * 租户切换/角色切换/个人中心/退出登录
 */
import React from 'react';
import { withRouter, routerRedux } from 'dva/router';
import { Icon, Avatar, Dropdown, Menu, Divider } from 'antd';
import { Bind } from 'lodash-decorators';
import { connect } from 'dva';
import intl from "../../../utils/intl";
import { openTab } from "../../../utils/menuTab";
import { getCurrentOrganizationId, isTenantRoleLevel } from "../../../utils/utils";
import DefaultRoleSelect from "../DefaultRoleSelect";
import DefaultDataHierarchies from "../DefaultDataHierarchies";
import DefaultDataHierarchiesSelect from "../DefaultDataHierarchiesSelect";
import styles from "./styles.less?modules";
import defaultUserAvatar from "../../../assets/logo-usercenter-default.png";
var DefaultCommonSelect = (_dec = Bind(), _dec2 = Bind(), (_class = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(DefaultCommonSelect, _React$PureComponent);

  var _super = _createSuper(DefaultCommonSelect);

  function DefaultCommonSelect() {
    var _this;

    _classCallCheck(this, DefaultCommonSelect);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      dropdownVisible: false,
      userAvatar: defaultUserAvatar
    };

    _this.handleMenuClick = function (_ref) {
      var key = _ref.key;

      if (key === 'triggerError') {
        _this.props.dispatch(routerRedux.push('/exception/trigger'));

        return;
      }

      if (key === 'logout') {
        _this.props.dispatch({
          type: 'login/logout'
        });
      }

      if (key !== 'role') {
        _this.setState({
          dropdownVisible: false
        });
      }
    };

    _this.handleVisibleChange = function (flag) {
      _this.setState({
        dropdownVisible: flag
      });
    };

    return _this;
  }

  _createClass(DefaultCommonSelect, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      // 由于是再个人中心数据加载完成后再加载的数据, 所以需要先设置 头像
      var _this$props = this.props,
          userAvatar = _this$props.currentUser.imageUrl,
          dispatch = _this$props.dispatch;

      if (userAvatar) {
        var img = new Image();
        img.onload = this.updateUserAvatar;
        img.onerror = this.setDefaultUserAvatar;
        img.src = userAvatar;
      }

      dispatch({
        type: 'user/fetchRoleList',
        payload: {
          organizationId: getCurrentOrganizationId()
        }
      });
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var prevUserAvatar = prevProps.currentUser && prevProps.currentUser.imageUrl;
      var nextUserAvatar = this.props.currentUser && this.props.currentUser.imageUrl;

      if (prevUserAvatar !== nextUserAvatar) {
        // 只有当 用户头像存在 才会设置 用户头像
        if (nextUserAvatar) {
          var img = new Image();
          img.onload = this.updateUserAvatar;
          img.onerror = this.setDefaultUserAvatar;
          img.src = nextUserAvatar;
        }
      }
    }
  }, {
    key: "updateUserAvatar",
    value: function updateUserAvatar() {
      var nextUserAvatar = this.props.currentUser && this.props.currentUser.imageUrl;
      this.setState({
        userAvatar: nextUserAvatar
      });
    }
  }, {
    key: "setDefaultUserAvatar",
    value: function setDefaultUserAvatar() {
      this.setState({
        userAvatar: defaultUserAvatar
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          currentUser = _this$props2.currentUser,
          _this$props2$roleList = _this$props2.roleList,
          roleList = _this$props2$roleList === void 0 ? [] : _this$props2$roleList,
          hierarchicalSelectList = _this$props2.hierarchicalSelectList,
          isModal = _this$props2.isModal,
          isSelect = _this$props2.isSelect;
      var userAvatar = this.state.userAvatar;
      var dropdownMenu = /*#__PURE__*/React.createElement(Menu, {
        className: styles.menu,
        selectedKeys: [],
        onClick: this.handleMenuClick
      }, roleList.length !== 0 && /*#__PURE__*/React.createElement(DefaultRoleSelect, null), /*#__PURE__*/React.createElement(Divider, {
        style: {
          width: '128px',
          margin: '4px auto'
        }
      }), /*#__PURE__*/React.createElement(Menu.Item, null, /*#__PURE__*/React.createElement("a", {
        style: {
          color: '#666'
        },
        href: "#/ciam/user/info",
        onClick: function onClick(e) {
          if (_isFunction(e && e.preventDefault)) {
            e.preventDefault();
          }

          openTab({
            title: 'cpaas.common.title.userInfo',
            key: '/ciam/user',
            path: '/ciam/user/info',
            icon: 'user',
            closable: true
          });
        }
      }, intl.get('cpaas.common.basicLayout.userInfo').d('个人中心'))), currentUser.dataHierarchyFlag && isTenantRoleLevel() && /*#__PURE__*/React.createElement(Divider, {
        style: {
          width: '128px',
          margin: '4px auto'
        }
      }), currentUser.dataHierarchyFlag && isModal && isTenantRoleLevel() && /*#__PURE__*/React.createElement(Menu.Item, {
        key: "data"
      }, /*#__PURE__*/React.createElement(DefaultDataHierarchies, null)), currentUser.dataHierarchyFlag && isSelect && isTenantRoleLevel() && hierarchicalSelectList.map(function (item) {
        return /*#__PURE__*/React.createElement(DefaultDataHierarchiesSelect, {
          dataHierarchyCode: item.dataHierarchyCode
        });
      }), /*#__PURE__*/React.createElement(Divider, {
        style: {
          width: '128px',
          margin: '4px auto'
        }
      }), /*#__PURE__*/React.createElement(Menu.Item, {
        key: "logout"
      }, /*#__PURE__*/React.createElement("div", {
        className: styles['user-logout']
      }, intl.get('cpaas.common.message.loginOut').d('退出登录'), /*#__PURE__*/React.createElement(Icon, {
        className: styles['logout-icon']
      }))));
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Dropdown, {
        overlay: dropdownMenu,
        trigger: ['click'],
        onVisibleChange: this.handleVisibleChange,
        visible: this.state.dropdownVisible
      }, /*#__PURE__*/React.createElement("span", {
        className: this.state.dropdownVisible ? "".concat(styles['dropdown-open'], " ").concat(styles.account) : "".concat(styles.action, " ").concat(styles.account)
      }, /*#__PURE__*/React.createElement(Avatar, {
        size: "small",
        className: styles.avatar,
        src: userAvatar
      }), /*#__PURE__*/React.createElement("span", {
        className: styles.name
      }, currentUser.realName || currentUser.currentRoleName || '', " ", /*#__PURE__*/React.createElement(Icon, {
        type: "down"
      })))));
    }
  }]);

  return DefaultCommonSelect;
}(React.PureComponent), (_applyDecoratedDescriptor(_class.prototype, "updateUserAvatar", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "updateUserAvatar"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "setDefaultUserAvatar", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "setDefaultUserAvatar"), _class.prototype)), _class));
export default withRouter(connect(function (_ref2) {
  var _ref2$user = _ref2.user,
      user = _ref2$user === void 0 ? {} : _ref2$user;
  return {
    currentUser: user.currentUser,
    roleList: user.roleList,
    hierarchicalSelectList: user.hierarchicalSelectList,
    // 数据层级下拉框列表
    isModal: user.isModal,
    // 是否显示模态框形式数据层级配置
    isSelect: user.isSelect // 是否显示下拉框形式数据层级配置

  };
})(DefaultCommonSelect));